package bid.yanhj.springboot.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import bid.yanhj.springboot.po.User;

@Controller
public class UserController {

    /**
     * jsp-demo
     * @param model
     * @return
     */
    @RequestMapping("/showUser")
    public String showUser(Model model) {
        List<User> list = new ArrayList<>();
        list.add(new User(1, "张三", 20));
        list.add(new User(1, "张三2", 22));
        list.add(new User(1, "张三3", 23));

        model.addAttribute("list", list);
        return "userlist";
    }

    /**
     * freemarker - demo
     * @param model
     * @return
     */
    @RequestMapping("/showUserFr")
    public String showUserFr(Model model) {
        List<User> list = new ArrayList<>();
        list.add(new User(1, "张三", 20));
        list.add(new User(1, "张三2", 22));
        list.add(new User(1, "张三3", 23));

        model.addAttribute("list", list);
        return "userlistf";
    }
    
    /**
     * thymeleaf demo
     * @param model
     * @return
     */
    @RequestMapping("/showUserThy")
    public String showUserThy(Model model) {
        List<User> list = new ArrayList<>();
        list.add(new User(1, "张三", 20));
        list.add(new User(1, "张三2", 22));
        list.add(new User(1, "张三3", 23));

        String mString = "T我是一个thymeleaf";
        model.addAttribute("list", list);
        model.addAttribute("msg", mString);
        model.addAttribute("userDate", new Date());
        model.addAttribute("sex", '1');
        return "userlistthy";
    }
    
    /**
     * thymeleaf demo
     * @param model
     * @return
     */
    @RequestMapping("/showUserThyReq")
    public String showUserThyReq(HttpServletRequest request,Model model) {
        request.setAttribute("req", "HttpServletRequest");
        request.getSession().setAttribute("sess", "HttpSession");
        request.getSession().getServletContext().setAttribute("app", "Application");
        return "userlistthyreq";
    }
    
    @RequestMapping("/showBaidu")
    public String showBaidu(Model model) {

        return "showBaidu";
    }
    
    
    ////////////////////// 异常处理
    
    @RequestMapping("/show")
    public String showInfo(){
        String str = null;
        str.length();
        return "index";
    }
    
    @RequestMapping("/show2")
    public String showInfo2(){
        int a = 10/0;
        return "index";
    }
    
//    /**
//     * java.lang.ArithmeticException
//     * 该方法需要返回一个ModelAndView：目的是可以让我们封装异常信息以及视图的指定
//     * 参数Exception e:会将产生异常对象注入到方法中
//     */
//    @ExceptionHandler(value={java.lang.ArithmeticException.class})
//    public ModelAndView arithmeticExceptionHandler(Exception e){
//        ModelAndView mv = new ModelAndView();
//        mv.addObject("error", e.toString());
//        mv.setViewName("error1");
//        return mv;
//    }
//    
//    /**
//     * java.lang.NullPointerException
//     * 该方法需要返回一个ModelAndView：目的是可以让我们封装异常信息以及视图的指定
//     * 参数Exception e:会将产生异常对象注入到方法中
//     */
//    @ExceptionHandler(value={java.lang.NullPointerException.class})
//    public ModelAndView nullPointerExceptionHandler(Exception e){
//        ModelAndView mv = new ModelAndView();
//        mv.addObject("error", e.toString());
//        mv.setViewName("error2");
//        return mv;
//    }
}
